package monster.com.cvh.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import monster.com.cvh.R;
import monster.com.cvh.activity.DownAndSendResumeActivity;
import monster.com.cvh.constant.Constans;
import monster.com.cvh.listener.EmailDialogListener;
import monster.com.cvh.util.KeyboardUtils;
import monster.com.cvh.util.MyConstant;

/**
 * @author: 13580
 * @data: 2018/3/19
 * @function:下载简历DialogFragment
 */

public class DownResumeDialogFragment extends DialogFragment implements View.OnClickListener {
    private ImageView mIvClose;
    private EditText mEdtFileName;
    private TextView mTvEmail;
    private TextView mTvDownload;
    private int mTemplateId = 1;
    private View view;
    private static final String TAG = "DownResumeDialogFragmen";
    private EmailDialogListener mListener;

    public void setOnShowEmailListener(EmailDialogListener mListener) {
        this.mListener = mListener;
    }

    public DownResumeDialogFragment newInstance(int templateId) {
        Bundle args = new Bundle();
        args.putInt(MyConstant.TEMPLATE_ID, templateId);
        DownResumeDialogFragment fragment = new DownResumeDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mTemplateId = args.getInt(MyConstant.TEMPLATE_ID);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.down_resume_dialog_fragment);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(attributes);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.down_resume_dialog_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(view);
        initEvent(view);
    }

    /**
     * 绑定控件事件
     *
     * @param view
     */
    private void initEvent(View view) {
        mIvClose.setOnClickListener(this);
        mTvEmail.setOnClickListener(this);
        mTvDownload.setOnClickListener(this);
    }

    /**
     * 绑定控件
     *
     * @param view
     */
    private void initView(View view) {
        mIvClose = (ImageView) view.findViewById(R.id.iv_dialog_fragment_down_resume_close);
        mEdtFileName = (EditText) view.findViewById(R.id.edt_dialog_fragment_down_resume_file_name);
        mTvEmail = (TextView) view.findViewById(R.id.tv_dialog_fragment_down_resume_email);
        mTvDownload = (TextView) view.findViewById(R.id.tv_dialog_fragment_down_resume_downlaod);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_dialog_fragment_down_resume_close:
                KeyboardUtils.hideSoftInput(view);
                dismiss();
                break;
            case R.id.tv_dialog_fragment_down_resume_downlaod:
                KeyboardUtils.hideSoftInput(view);
                String fileName = mEdtFileName.getText().toString();
                dismiss();
                Intent intent = new Intent(getContext(), DownAndSendResumeActivity.class);
                intent.putExtra(MyConstant.TEMPLATE_ID,mTemplateId);
                intent.putExtra(MyConstant.FILE_NAME, fileName);
                intent.putExtra(Constans.DOWN_RESUME, Constans.DOWN_LOAD_RESUME);
                startActivity(intent);
                break;
            case R.id.tv_dialog_fragment_down_resume_email:
                KeyboardUtils.hideSoftInput(view);
                mListener.onShowEmailDialog();
                dismiss();
                break;
        }
    }
}


